//
//  YLFuncDetailViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/25.
//  Copyright © 2017年 she1323. All rights reserved.
//  产品详情

#import "YLFuncDetailViewModel.h"
#import "YLFuncListModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import <YYModel.h>
@implementation YLFuncDetailViewModel

- (instancetype)init
{
    if (self = [super init]) {
        _callPhoneSubject = [RACSubject subject];
    }
    return self;
}


- (void)setListModel:(YLFuncListModel *)listModel
{
    _listModel = listModel;
    
    // 设置头部内容
    {
        NSString *durationStr = listModel.duration;
        if (listModel.extendedPeriodMonth) {
            if ([listModel.extendedPeriodMonth integerValue] == 0) {
            } else {
                durationStr = [NSString stringWithFormat:@"%@+%@", listModel.duration, listModel.extendedPeriodMonth];
            }
            
        }
        
        NSString *investmentAmountStr = @"";
        if (listModel.investmentAmount > 9999) {
            investmentAmountStr = [NSString stringWithFormat:@"%.2f(万)", listModel.investmentAmount / 10000.0];
        } else {
            investmentAmountStr = [NSString stringWithFormat:@"%.2f", listModel.investmentAmount];
        }
        _detailHeaderTitles = @[[YLFuncDetailHeaderModel modelWithContent:listModel.productTypeText      title:@"基金类型"],
                                
                                [YLFuncDetailHeaderModel modelWithContent:[NSString stringWithFormat:@"%@(月)", durationStr] title:@"存续期限"],
                                [YLFuncDetailHeaderModel modelWithContent:investmentAmountStr title:@"起投金额"]];
    }
    
    
    // 设置详情内容
    {
        __weak typeof(self) weakSelf = self;
        YLFuncDetailModelGroup *group1 = [[YLFuncDetailModelGroup alloc] init];
        
        // 产品规模
        NSString *productSizeStr = @"";
        if (listModel.productSize > 9999) {
            productSizeStr = [NSString stringWithFormat:@"%.2f(万)", listModel.productSize / 10000.0];
        } else {
            productSizeStr = [NSString stringWithFormat:@"%.2f", listModel.productSize];
        }
        
        group1.detailLists = @[[YLFuncDetailModel modelWithContent:productSizeStr title:@"产品规模" icon:@"productScale"],
                               [YLFuncDetailModel modelWithContent:listModel.operationModeText title:@"运作方式" icon:@"operationMode"],
                               [YLFuncDetailModel modelWithContent:listModel.raiseStartText title:@"基金募集起始时间" icon:@"collection"],
                               [YLFuncDetailModel modelWithContent:listModel.managerName title:@"基金管理人" icon:@"administrator"],];
        
        YLFuncDetailModelGroup *group2 = [[YLFuncDetailModelGroup alloc] init];
        group2.detailLists = @[[YLFuncDetailModel modelWithContent:@"400-6286-188" title:@"咨询电话" icon:@"advisoryTelephone" Clickblock:^{
            [weakSelf.callPhoneSubject sendNext:@"400-6286-188"];
        }]];
            
        _detailLists = @[group1, group2];
    }}


- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                
                NSString *url = [NSString stringWithFormat:@"%@Product/PGetFundProductDetail", YLURL];
                NSDictionary *param = @{@"Id" : weakSelf.listModel.fundProductId};
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        weakSelf.listModel = [YLFuncListModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _requestCommand;
}

- (RACCommand *)appointmentCommand
{
    if (!_appointmentCommand) {
        __weak typeof(self) weakSelf = self;
        _appointmentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               
                NSString *url = [NSString stringWithFormat:@"%@Product/FundProductReservation", YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:@{@"Id" : weakSelf.listModel.fundProductId} success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                    
                }];
                
                
                return nil;
            }];
            
        }];
        
        
        
    }
    return _appointmentCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
}
    
    





@end
